package com.pentofab.payrollmanagement.ui.controller;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Parent;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;

import com.pentofab.payrollmanagement.application.PayrollApplicationFactory;
import com.pentofab.payrollmanagement.dao.EmployeeDao;
import com.pentofab.payrollmanagement.dao.model.BankInfo;
import com.pentofab.payrollmanagement.dao.model.Company;
import com.pentofab.payrollmanagement.exception.DatabaseException;
import com.pentofab.payrollmanagement.utilities.ValidationUtilities;

public class AddBankInfoUIController extends AbstractUIController implements
		FormValidator {
	private Company company;
	private BankInfo bankInfo;
	private boolean updateMode = false;	

    @FXML
    private TextField ISFCCodeField;

    @FXML
    private TextField addressLineField;

    @FXML
    private TextField bankCodeField;

    @FXML
    private TextField bankNameField;

    @FXML
    private TextField cityField;

    @FXML
    private TextField countryField;

    @FXML
    private TextField emailField;

    @FXML
    private TextField faxField;

    @FXML
    private GridPane root;

    @FXML
    private TextField stateField;

    @FXML
    private TextField telephone1Field;

    @FXML
    private TextField telephone2Field;

    @FXML
    private TextField zipCodeField;
    
	@Override
	public Parent getView() {
		return root;
	}

	@Override
	public String getTitle() {
		return UIMessages.UITitles.ADD_UPDATE_COMPANY;
	}

	public void save() {		
		String validationMessage = getValidationMessage();
		if (!validationMessage.equals("")) {
			showError(validationMessage);
			return;
		}
		
		else {
			bankInfo = new BankInfo();
			bankInfo.setName(bankNameField.getText());
			bankInfo.setBankCode(bankCodeField.getText());
			bankInfo.setAddress(addressLineField.getText());
			bankInfo.setISFCCode(ISFCCodeField.getText());
			bankInfo.setCity(cityField.getText());
			bankInfo.setState(stateField.getText());
			bankInfo.setCountry(countryField.getText());
			bankInfo.setZipcode(zipCodeField.getText());
			bankInfo.setTelephone1(telephone1Field.getText());
			bankInfo.setTelephone2(telephone2Field.getText());
			bankInfo.setFax(faxField.getText());
			bankInfo.setMailId(emailField.getText());
			EmployeeDao empDao = PayrollApplicationFactory.getEmployeeMgmt();
			System.out.println("BankInfo ; "+bankInfo);
			try {
				if (!updateMode) {
					empDao.addBank(bankInfo);
					refreshUI();
					showInfo(String.format(UIMessages.BANK_ADD_SUCCESS,
							bankInfo.getName()));
				} else {
					empDao.updateBankInfo(bankInfo);
					showInfo(String.format(UIMessages.BANK_UPDATE_SUCCESS,
							bankInfo.getName()));
				}
			} catch (DatabaseException e) {
				e.printStackTrace();
				String message = "";
				switch (e.getCode()) {
				case DatabaseException.DUPLICATE_ENTRY:
					message = UIMessages.DUPLICATE_COMPANY_CODE_ERROR_MESSAGE;
					break;
				case DatabaseException.UNKNOWN_ERROR:
				default:
					message = UIMessages.UNKNOW_DATABASE_ERROR;
					break;
				}
				showError(message);
			} catch (Exception e) {
				e.printStackTrace();
				showError(UIMessages.UNKNOW_DATABASE_ERROR);
			}
		}
	}

	public void goBack() {
		if (getParent() != null && getParent().length() > 0) {
			PayrollApplicationFactory.getMainController().showScreen(
					getParent(), false);
		}
	}

	public void reset() {
		
		refreshUI();
	}

	@Override
	public boolean isValid() {
		if (bankNameField.getText().length() < 2) {
			return false;
		}

		if (bankCodeField.getText().length() != 2) {
			return false;
		}
		if (addressLineField.getText().length() < 2) {
			return false;
		}

		if (cityField.getText().length() < 2) {
			return false;
		}

		if (stateField.getText().length() < 2) {
			return false;
		}

		if (countryField.getText().length() < 2) {
			return false;
		}

		if (zipCodeField.getText().length() < 5) {
			return false;
		}

		if (telephone1Field.getText().length() < 5) {
			return false;
		}

		if (!ValidationUtilities.validateEmail(emailField.getText())) {
			return false;
		}

		return true;
	}

	@Override
	public String getValidationMessage() {

		if (bankNameField.getText().length() < 2) {
			return UIMessages.COMPANY_NAME_VALIDATION_MESSAGE;
		}

		if (bankCodeField.getText().length() != 2) {
			return UIMessages.COMPANY_CODE_VALIDATION_MESSAGE;
		}
		if (addressLineField.getText().length() < 2) {
			return UIMessages.COMPANY_ADDRESS_VALIDATION_MESSAGE;
		}

		if (cityField.getText().length() < 2) {
			return UIMessages.COMPANY_CITY_VALIDATION_MESSAGE;
		}

		if (stateField.getText().length() < 2) {
			return UIMessages.COMPANY_STATE_VALIDATION_MESSAGE;
		}

		if (countryField.getText().length() < 2) {
			return UIMessages.COMPANY_COUNTRY_VALIDATION_MESSAGE;
		}

		if (zipCodeField.getText().length() < 5) {
			return UIMessages.COMPANY_ZIPCODE_VALIDATION_MESSAGE;
		}

		if (telephone1Field.getText().length() < 5) {
			return UIMessages.COMPANY_TELEPHONE_VALIDATION_MESSAGE;
		}
		/*if (!ValidationUtilities.validateEmail(emailField.getText())) {
			return UIMessages.COMPANY_EMAIL_VALIDATION_MESSAGE;
		}*/
		return "";

	}

	@Override
	public void refreshUI(Object... args) {
		
		if (args != null && args.length > 0) {
			company = (Company) args[0];
		}
		if (args != null && args.length > 1) {
			BankInfo bankInfo = (BankInfo) args[1];
			System.out.println("bankInfo:" + bankInfo);			
			this.bankInfo = bankInfo;
			updateMode = true;
			bankNameField.setText(bankInfo.getName());
			bankCodeField.setText(bankInfo.getBankCode());
			bankCodeField.disableProperty().set(true);
			addressLineField.setText(bankInfo.getAddress());
			ISFCCodeField.setText(bankInfo.getISFCCode());
			cityField.setText(bankInfo.getCity());
			stateField.setText(bankInfo.getState());
			countryField.setText(bankInfo.getCountry());
			zipCodeField.setText(bankInfo.getZipcode());
			telephone1Field.setText(bankInfo.getTelephone1());
			telephone2Field.setText(bankInfo.getTelephone2());
			faxField.setText(bankInfo.getFax());
			emailField.setText(bankInfo.getMailId());
		} else {
			updateMode = false;
			bankNameField.clear();
			bankCodeField.clear();
			addressLineField.clear();
			ISFCCodeField.clear();
			cityField.clear();
			stateField.clear();
			countryField.clear();
			zipCodeField.clear();
			telephone1Field.clear();
			telephone2Field.clear();
			faxField.clear();
			emailField.clear();
		}
	}
}
